<?php

namespace App\Http\Controllers;

use App\Http\Requests\Article;
use App\Models\Articles;
use App\Models\Type;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = new Articles();
        if ($request->type_id){
           $query = $query->where('type_id', $request->type_id);
        }
        if ($request->search_name){
            $query = $query->where('name', 'like', "%" . $request->search_name . "%");
        }
        $articles = $query->paginate(10);
        $types = Type::all();
        return view('article.index',compact('articles', 'types'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $types = Type::all();
        $article = null;
        return view('article.create',compact('types', 'article'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Article $request)
    {
        $validate = $request->validated();
        $value = json_decode($validate['value'], true);
        $value = self::handleValue($value);
        session(['article_type_id' => $validate['type_id'] ?? "", 'article_value' => $value ?? ""]);
        $res = Articles::create($validate);
        return redirect()->route('articles.index')->with($res ? 'success' : 'danger', $res ? "新建接口成功" : "新建接口失败");
    }

    private function handleValue($value){
        $data = [];
        if (!$value || !is_array($value)) return $data;
        foreach ($value as $k => $v){
            if (is_array($v)){
                $data[$k] = $this->handleValue($v);
            } else{
                $data[$k] = "";
            }
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param Articles $article
     * @return \Illuminate\Http\Response
     */
    public function show(Articles $article)
    {
        return view('article.show', compact('article'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Articles $article
     * @return \Illuminate\Http\Response
     */
    public function edit(Articles $article)
    {
        $types = Type::all();
        return view('article.create', ['article' => $article, 'types' => $types]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Article $request
     * @param Articles $article
     * @return \Illuminate\Http\Response
     */
    public function update(Article $request, Articles $article)
    {
        $validate = $request->validated();
        $res = $article->update($validate);
        return redirect()->route('articles.index')->with(  $res ? 'success' : 'danger',   $res ? '编辑接口成功' : '编辑接口失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        $article = Articles::find($id);
        $article->delete();
        return \response()->json(['code' => 200, 'msg' => "删除成功"]);
    }
}
